//
// Created by almamu on 4/04/19.
//

#ifndef WALLENGINE_CPKGREADER_H
#define WALLENGINE_CPKGREADER_H

#include <irrlicht/irrlicht.h>
#include <string>

#include "CFileList.h"

//! Archiveloader capable of loading WAD Archives
class CArchiveLoaderPkg : public irr::io::IArchiveLoader
{
public:

    //! Constructor
    CArchiveLoaderPkg(irr::io::IFileSystem* fs);

    //! returns true if the file maybe is able to be loaded by this class
    //! based on the file extension (e.g. ".zip")
    virtual bool isALoadableFileFormat(const irr::io::path& filename) const;

    //! Check if the file might be loaded by this class
    /** Check might look into the file.
    \param file File handle to check.
    \return True if file seems to be loadable. */
    virtual bool isALoadableFileFormat(irr::io::IReadFile* file) const;

    //! Check to see if the loader can create archives of this type.
    /** Check based on the archive type.
    \param fileType The archive type to check.
    \return True if the archile loader supports this type, false if not */
    virtual bool isALoadableFileFormat(irr::io::E_FILE_ARCHIVE_TYPE fileType) const;

    //! Creates an archive from the filename
    /** \param file File handle to check.
    \return Pointer to newly created archive, or 0 upon error. */
    virtual irr::io::IFileArchive* createArchive(const irr::io::path& filename, bool ignoreCase, bool ignorePaths) const;

    //! creates/loads an archive from the file.
    //! \return Pointer to the created archive. Returns 0 if loading failed.
    virtual irr::io::IFileArchive* createArchive(irr::io::IReadFile* file, bool ignoreCase, bool ignorePaths) const;

private:
    irr::io::IFileSystem* FileSystem;
};

class CPkgReader : public virtual irr::io::IFileArchive, virtual irr::io::CFileList
{
public:

    //! constructor
    CPkgReader (irr::io::IReadFile* file, bool ignoreCase, bool ignorePaths);

    //! destructor
    virtual ~CPkgReader ();

    //! opens a file by file name
    virtual irr::io::IReadFile* createAndOpenFile (const irr::io::path& filename);

    //! opens a file by index
    virtual irr::io::IReadFile* createAndOpenFile (unsigned int index);

    //! returns the list of files
    virtual const IFileList* getFileList () const;

    //! get the archive type
    virtual irr::io::E_FILE_ARCHIVE_TYPE getType () const;

protected:
    void scanPkgHeader ();

    char* readSizedString ();

    irr::io::IFileSystem* mFileSystem;
    irr::io::IReadFile* mFile;
};


#endif //WALLENGINE_CPKGREADER_H
